package source;

import java.util.Calendar;

public class MyDate {
	private int year;
	private int month;
	private int day;
	private boolean leap = false;
	private byte[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	
	public MyDate() {
		Calendar today = Calendar.getInstance();
		this.year = today.get(Calendar.YEAR);
		this.month = today.get(Calendar.MONTH) + 1;
		this.day = today.get(Calendar.DAY_OF_MONTH);
		/* check if a leap year */
		if ((0 == year%400) || (0 != year%100 && 0 == year%4)) {
			leap = true;
		}
		if (leap) {
			daysOfMonth[1] = 29;
		}
	}
	
	public MyDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
		/* check if a leap year */
		if ((0 == year%400) || (0 != year%100 && 0 == year%4)) {
			leap = true;
		}
		if (leap) {
			daysOfMonth[1] = 29;
		}
	}
	
	public boolean isValid() {
		if (day > daysOfMonth[month-1]) {
			System.out.println("Error : the month " + month + " of year " + year + " only has " + daysOfMonth[month-1] + " days.");
			return false;
		}

		return true;
	}

	public MyDate nextDay() throws Exception {
		day++;
		if (day > daysOfMonth[month-1]) {
			day = 1;
			month++;
		}
		if (month > 12) {
			month = 1;
			year++;
		}
		if (year < 0) {
			throw new Exception("The year of next day of this date overflows!");
		}
		return new MyDate(year, month, day);
	}
	
	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}

}
